package io.renren.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.R;
import io.renren.modules.shop.entity.GoodsUserPosition;
import io.renren.modules.shop.service.GoodsUserPositionService;
import io.renren.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("goods_user_position")
@Api(value = "收货地址Controller",tags = {("收获地址接口")})
@Log4j2
public class GoodsUserPositionController extends AbstractController {
    @Autowired
    private GoodsUserPositionService goodsUserPositionService;

    @ApiOperation(value = "查询分页")
    @PostMapping("page")
    public R getPage(@RequestBody GoodsUserPosition goodsUserPosition, @RequestParam Integer size, @RequestParam Integer page) {
        IPage<GoodsUserPosition> iPage = new Page<>(page,size);
        IPage<GoodsUserPosition> goodsCouponIPage = goodsUserPositionService.page(iPage, new QueryWrapper<>(goodsUserPosition));
        List<GoodsUserPosition> list = goodsCouponIPage.getRecords();
        return R.ok().put("page",list);
    }


    @ApiOperation(value = "新增详情订单")
    @GetMapping("add")
    public R add(@RequestParam String pTel,@RequestParam String pPlace) {
        GoodsUserPosition goodsUserPosition = new GoodsUserPosition();
        goodsUserPosition.setPTel(pTel);
        goodsUserPosition.setPPlace(pPlace);
        goodsUserPosition.setOpenid(getOpenId());
        log.info(goodsUserPosition.toString() + "这是传递进来的参数");
        boolean flag = goodsUserPositionService.save(goodsUserPosition);
        return R.ok().put("flag",flag);
    }

    @ApiOperation(value = "新增详情订单")
    @GetMapping("update")
    public R update(@RequestParam Integer id,@RequestParam String pTel,@RequestParam String pPlace) {
        GoodsUserPosition goodsUserPosition = new GoodsUserPosition();
        goodsUserPosition.setPid(id);
        goodsUserPosition.setPTel(pTel);
        goodsUserPosition.setPPlace(pPlace);
        goodsUserPosition.setOpenid(getOpenId());
        log.info(goodsUserPosition.toString() + "这是传递进来的参数");
        boolean flag = goodsUserPositionService.updateById(goodsUserPosition);
        return R.ok().put("flag",flag);
    }

    @ApiOperation(value = "查询所有数据")
    @PostMapping("list")
    public R getList(@RequestBody GoodsUserPosition goodsUserPosition) {
        goodsUserPosition.setOpenid(getOpenId());
        List<GoodsUserPosition> list = goodsUserPositionService.list(new QueryWrapper<>(goodsUserPosition));
        return R.ok().put("list",list);
    }



    @ApiOperation(value = "根据ids删除记录")
    @PostMapping("delete")
    public R del(@RequestParam Integer[] ids) {
        boolean flag = goodsUserPositionService.removeByIds(Arrays.asList(ids));
        return R.ok().put("flag",flag);
    }

    @ApiOperation(value = "根据id删除记录")
    @GetMapping("del")
    public R delById(@RequestParam Integer id) {
        boolean flag = goodsUserPositionService.removeById(id);
        return R.ok().put("flag",flag);
    }

    @ApiOperation(value = "设置默认收货位置")
    @GetMapping("default")
    public R setDefault(@RequestParam Integer id) {
        GoodsUserPosition goodsUserPosition = goodsUserPositionService.getById(id);
        goodsUserPosition.setPType(1);
        boolean flag = goodsUserPositionService.updateById(goodsUserPosition);
        return R.ok().put("flag",flag);
    }



}
